const router = require('koa-router')()
const JWT = require('jsonwebtoken')
const userList = [{
  "username": "13111111111",
  "password": "123456",
  "avatar": "https://robohash.org/123?set=set5",
  "nickname": "匿名用户"
},{
  "username": "13222222222",
  "password": "123456",
  "avatar": "https://robohash.org/1231231231?set=set5",
  "nickname": "匿名用户123"
}]

router.post('/login', ctx => {
  const { username, password } = ctx.request.body;
  const item = userList.find(v => v.username === username && v.password === password)

  if(item) {
    ctx.body = {
      code: 200,
      msg: '登录成功',
      data: {
        info: item,
        token: JWT.sign(item, 'qweqeq')
      }
    }
  } else {
    ctx.body = {
      code: 403,
      msg: '登录失败， 重新登录'
    }
  }
})
module.exports = router
